#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 205;

int n, m;
char g[N][N];
int vis[N][N];
int hexval(char c) {
  if (isdigit(c))
    return c - '0';
  else
    return c - 'a' + 10;
}
char decode(int zero) {
  static string s = "WAKJSD";
  return s[zero];
}
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int zero;
void dfs(int x, int y) {
  if (g[x][y] != '.') return;
  if (vis[x][y]) return;
  vis[x][y] = 1;
  rep(i, 0, 3) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (vis[nx][ny]) continue;
    if (g[nx][ny] != '.') continue;
    dfs(nx, ny);
  }
}
void dfs2(int x, int y) {
  if (g[x][y] != '#') return;
  if (vis[x][y]) return;
  vis[x][y] = 1;
  rep(i, 0, 3) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
    if (vis[nx][ny]) continue;
    if (g[nx][ny] == '.')
      zero++, dfs(nx, ny);
    else
      dfs2(nx, ny);
  }
}
void solve(int id) {
  string s;
  rep(i, 1, n) {
    cin >> s;
    rep(j, 0, m - 1) {
      int x = hexval(s[j]);
      rep(k, 1, 4) g[i][j * 4 + k] = ((x >> (4 - k)) & 1) ? '#' : '.';
    }
  }
  m *= 4;
  rep(i, 1, n) rep(j, 1, m) vis[i][j] = 0;
  // 处理背景
  rep(j, 1, m) dfs(1, j), dfs(n, j);
  rep(i, 1, n) dfs(i, 1), dfs(i, m);
  string res;

  rep(i, 1, n) rep(j, 1, m) {
    if (!vis[i][j] && g[i][j] == '#') {
      zero = 0;
      dfs2(i, j);
      res.push_back(decode(zero));
    }
  }
  sort(res.begin(), res.end());
  cout << "Case " << id << ": ";
  cout << res << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int id = 0;
  while (cin >> n >> m && n && m) solve(++id);
  return 0;
}